#include "stdafx.h"
#define MYLIB_API_EXPORT

#include "ConvexHull.h"

//initialize the static member
std::auto_ptr<CConvexHull> CConvexHull::_instance(0);

CConvexHull::CConvexHull(void)
{
}

CConvexHull::~CConvexHull(void)
{
}

void CConvexHull::ConvexModel(ModelOBJ* model)
{
	for (SOLID_itr solid_itr = model->begin(); solid_itr != model->end();solid_itr++)
	{
		SolidOBJ* solid = *solid_itr;
		if (solid->Solid_mesh->size_of_vertices() == 0)
		{
			CGAL_assertion(0);
			continue;
		}

		//get the first mtl
		Face_itr fc = solid->Solid_mesh->facets_begin();
		simple_material_obj curmtl = fc->mtl;
		Polyhedron_3* ch_obj = new Polyhedron_3;
		CGAL::convex_hull_incremental_3(solid->Solid_mesh->points_begin(),solid->Solid_mesh->points_end(),*ch_obj);
		if (ch_obj->is_closed())
		{
			//set the mtl
			for (Face_itr itr = ch_obj->facets_begin(); itr != ch_obj->facets_end(); ++itr)
			{
				itr->mtl = curmtl;
			}
			//alternation
			Polyhedron_3* tmp = solid->Solid_mesh;
			solid->Solid_mesh = ch_obj;
			delete tmp;
			tmp = NULL;
		}
		else
		{
			CGAL_assertion(0);
		}
	}
}